package tech.dongliang.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import tech.dongliang.common.controller.CommonController;
import tech.dongliang.common.enums.EBizError;
import tech.dongliang.common.model.Tree;
import tech.dongliang.common.response.CommonResponse;
import tech.dongliang.common.response.ErrorData;
import tech.dongliang.system.model.SysMenu;
import tech.dongliang.system.service.ISysMenuService;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xdzhang
 * @since 2019-01-18
 */
@Controller
@RequestMapping("/system/menu")
public class SysMenuController extends CommonController {
    private String prefix = "system/menu";
    @Autowired
    private ISysMenuService sysMenuService;

    @GetMapping()
    String menu() throws Exception {
        return prefix + "/list";
    }

    @ResponseBody
    @GetMapping("/list")
    public CommonResponse<List<SysMenu>> list(@RequestParam(value = "menuName", required = false) String menuName) throws Exception {
        QueryWrapper query = new QueryWrapper();
        if (StringUtils.isNotBlank(menuName)) {
            query.eq("name", menuName);
        }
        List<SysMenu> list = sysMenuService.list(query);
        return CommonResponse.success(list);
    }

    @GetMapping("/add/{pid}")
    public String add(@PathVariable("pid") Long pid, Model model) throws Exception {
        model.addAttribute("pid", pid);
        if (pid == 0) {
            model.addAttribute("pName", "根目录");
        } else {
            model.addAttribute("pName", sysMenuService.getById(pid).getName());
        }
        return prefix + "/add";
    }

    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, Model model) throws Exception {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        model.addAttribute("sysMenu", sysMenu);
        if (0 == sysMenu.getPid().intValue()) {
            model.addAttribute("parentDeptName", "根目录");
        } else {
            SysMenu parDept = sysMenuService.getById(sysMenu.getPid());
            model.addAttribute("parentDeptName", parDept.getName());
        }
        return prefix + "/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public CommonResponse<Boolean> save(@Valid SysMenu sysMenu, BindingResult result) throws Exception {
        sysMenu.setCreateTime(System.currentTimeMillis());
        if (sysMenuService.save(sysMenu)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    /**
     * 修改
     */
    @ResponseBody
    @PostMapping("/update")
    public CommonResponse<Boolean> update(@Valid SysMenu sysMenu, BindingResult result) throws Exception {
        sysMenu.setUpdateTime(System.currentTimeMillis());
        if (sysMenuService.updateById(sysMenu)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public CommonResponse<Boolean> remove(Integer deptId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("parentId", deptId);
        if (sysMenuService.childCount(deptId) > 0) {
            return CommonResponse.error(new ErrorData(EBizError.MENU_HAS_CHILD));
        }
        if (sysMenuService.removeById(deptId)) {
            return CommonResponse.success(true);
        }
        return CommonResponse.error(new ErrorData(EBizError.UNKNOWE_ERROR));
    }

    @GetMapping("/tree")
    @ResponseBody
    public CommonResponse<Tree<SysMenu>> tree() throws Exception {
        Tree<SysMenu> tree = sysMenuService.getTree();
        return CommonResponse.success(tree);
    }

    @GetMapping("/tree/{roleId}")
    @ResponseBody
    CommonResponse<Tree<SysMenu>> tree(@PathVariable("roleId") Integer roleId) throws Exception {
        Tree<SysMenu> tree = sysMenuService.getTree(roleId);
        return CommonResponse.success(tree);
    }

    @GetMapping("/treeView")
    String treeView() throws Exception {
        return prefix + "/menuTree";
    }
}
